package com.beta.auth.utils;

import cn.hutool.crypto.symmetric.SymmetricCrypto;

import java.util.Base64;

public class SM4Helper2 {

    private SM4Helper2(){}

    //指明加密算法和秘钥
    private static SymmetricCrypto symmetricCrypto;

    public static synchronized void init(String key) {
        symmetricCrypto = new SymmetricCrypto("SM4/ECB/PKCS5Padding", key.getBytes());
    }

    //加密为16进制，也可以加密成base64/字节数组
    public static String encryptSm4(byte[] data) {
        byte[] encrypt = symmetricCrypto.encrypt(data);
        return new String(Base64.getEncoder().encode(encrypt));
    }

    //解密
    public static byte[] decryptSm4(byte[] data, String key) {
        return symmetricCrypto.decrypt(data);
    }
}
